package com.douqu.game.core.entity.ext;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.e.E_MailSenderType;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.entity.common.TwoTuple;
import com.douqu.game.core.entity.db.MailDB;
import com.douqu.game.core.entity.db.PrivacyDB;
import com.douqu.game.core.entity.ext.data.mail.DialogueData;
import com.douqu.game.core.factory.ConfigFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.WordFactory;
import com.douqu.game.core.protobuf.SGCommonProto;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author: Gavin.
 * Description:
 * Date: 2017/12/2 0002 下午 12:21
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
public class MailInfo extends ExtInfo{

    /***
     * 玩家邮件集合，key 类型 value 邮件列表
     */
    public Map<Integer,List<MailDB>> mailMap;

    public List<PrivacyDB> privacyList;

    public int mailNumber = 1;

    public MailInfo(Player player)
    {
        super(player);
        mailMap = new ConcurrentHashMap<>();
        privacyList = new ArrayList<>();
    }

    @Override
    public List<SGCommonProto.E_RED_POINT_TYPE> checkRedPointRemindAllCondition(long currentTime){

        if(checkReadPoint()){
            List<SGCommonProto.E_RED_POINT_TYPE> list = new ArrayList<>();
            list.add(SGCommonProto.E_RED_POINT_TYPE.RED_POINT_TYPE_EMAIL);
            return list;
        }
        return null;
    }

    @Override
    public void init() {
        //系统邮件开关
        if(ConfigFactory.gmSetting.isSendWelcomeMail()){
            String title = WordFactory.getWord(WordFactory.WELCOME_SYSTEM_MAIL_TITLE);
            String content = WordFactory.getWord(WordFactory.WELCOME_SYSTEM_MAIL_CONTENT);
            if(title != null && content != null)
                addMail(SGCommonProto.E_MAIL_TYPE.MAIL_TYPE_SYSTEM_VALUE,title,content,E_MailSenderType.MAIL_TYPE_RECEIVER.getCode(),player.getObjectIndex(),null,DataFactory.currentTime);
        }
    }

    @Override
    public void checkInit() {

    }

    @Override
    public void reset() {
//        mailNumber = 1;
//        mailMap.clear();
//        privacyList.clear();
    }

    @Override
    public void checkReset() {

    }

    @Override
    public void writeTo(ByteBuffer buffer) {
        buffer.writeInt(mailNumber);
        buffer.writeByte(privacyList.size());
        for (PrivacyDB privacyDB : privacyList){
            privacyDB.writeTo(buffer);
        }
        buffer.writeByte(mailMap.size());
        for(Map.Entry<Integer, List<MailDB>> entry : mailMap.entrySet())
        {
            buffer.writeByte(entry.getValue().size());
            for(MailDB mailDB : entry.getValue())
            {
                mailDB.writeTo(buffer);
            }
            buffer.writeByte(entry.getKey());
        }
    }


    public boolean checkReadPoint(){
        if (getMailReadPoint().size() > 0){
            return true;
        }
        return false;
    }

    public List<SGCommonProto.E_MAIL_TYPE> getMailReadPoint(){
        List<SGCommonProto.E_MAIL_TYPE> redPointList = new ArrayList<>();
        for(Map.Entry<Integer,List<MailDB>> entry : mailMap.entrySet()){
            if(entry.getKey() == SGCommonProto.E_MAIL_TYPE.MAIL_TYPE_SYSTEM_VALUE){
                for (MailDB mailDB : entry.getValue()){
                    if(!mailDB.isRead){
                        if (redPointList.contains(SGCommonProto.E_MAIL_TYPE.MAIL_TYPE_SYSTEM))
                            break;
                        redPointList.add(SGCommonProto.E_MAIL_TYPE.MAIL_TYPE_SYSTEM);
                        break;
                    }
                }
            }else if (entry.getKey() == SGCommonProto.E_MAIL_TYPE.MAIL_TYPE_RECHARGE_VALUE){
                for (MailDB mailDB : entry.getValue()){
                    if(!mailDB.isRead){
                        if (redPointList.contains(SGCommonProto.E_MAIL_TYPE.MAIL_TYPE_RECHARGE))
                            break;
                        redPointList.add(SGCommonProto.E_MAIL_TYPE.MAIL_TYPE_RECHARGE);
                        break;
                    }
                }
            }
        }

        for(PrivacyDB privacyDB : privacyList){
            if (!privacyDB.isRead){
                redPointList.add(SGCommonProto.E_MAIL_TYPE.MAIL_TYPE_PRIVACY);
                break;
            }
        }
        return redPointList;
    }

    @Override
    public void loadFrom(ByteBuffer buffer) {
        mailNumber = buffer.readInt();
        PrivacyDB privacyDB = null;
        MailDB mailDB = null;
        int i,j,size,keySize;
        size = buffer.readByte();
        for (i = 0 ; i < size; i++){
            privacyDB = new PrivacyDB();
            privacyDB.loadFrom(buffer);
            privacyList.add(privacyDB);
        }
        size = buffer.readByte();
        List<MailDB>  list = null;
        for(i = 0; i < size; i++){
            keySize = buffer.readByte();
            list = new CopyOnWriteArrayList<>();
            for( j = 0 ; j < keySize ; j++){
                mailDB = new MailDB();
                mailDB.loadFrom(buffer);
                list.add(mailDB);
            }
            mailMap.put(buffer.readByte(),list);
        }
    }

    public MailDB getMail(int type, int mailId)
    {
        List<MailDB> list = mailMap.get(type);
        if(list != null)
        {
            for(MailDB mailDB : list)
            {
                if(mailDB.id == mailId)
                    return mailDB;
            }
        }
        return null;
    }


    public List<MailDB> getMailList(int key){

        return mailMap.get(key);
    }

    public List<MailDB> getMailList()
    {
        List<MailDB> result = new CopyOnWriteArrayList<>();
        for(List<MailDB> mailDBList : mailMap.values())
        {
            result.addAll(mailDBList);
        }
        return result;
    }

    public void addMail(int type, MailDB mailDB)
    {
        List<MailDB> mailList = mailMap.get(type);
        if(mailList == null){
            mailList = new CopyOnWriteArrayList<>();
            mailMap.put(type,mailList);
        }

        if(mailDB.id == 0)
            mailDB.id = ++mailNumber;

        mailList.add(mailDB);

        if(mailList.size() > 50){
            mailList.remove(0);
        }
    }

    /**
     * 发送邮件
     * @param type
     * @param title
     * @param content
     * @param attachList
     */
    public void addMail (int type, String title, String content, int roleType, String target, List<GoodsData> attachList, long currentTime){
        List<MailDB> mailList = mailMap.get(type);
        if(mailList == null){
            mailList = new CopyOnWriteArrayList<>();
            mailMap.put(type,mailList);
        }
        MailDB mailDB = new MailDB();
        mailDB.id = ++mailNumber;
        mailDB.title = title;
        mailDB.content = content;
        mailDB.roleType = roleType;
        mailDB.target = target;
        mailDB.isGet = false;
        mailDB.isRead = false;
        mailDB.sendTime = currentTime;
        mailDB.setAttachList(attachList);
        mailList.add(mailDB);

        if(mailList.size() > 50){
            mailList.remove(0);
        }

        checkRedPointRemindAllCondition(DataFactory.currentTime);

    }

    /***
     * 发送私信
     * @param content
     */
//    public void sendPrivacy(PlayerController playerController,String content,Player receiverPlayer){
//        //发送方存储
////        sendPrivacy(E_MailSenderType.MAIL_TYPE_SENDER.getCode(),playerController.getPlayer(),content);
////        //接收方存储
////        sendPrivacy(E_MailSenderType.MAIL_TYPE_RECEIVER.getCode(),receiverPlayer,content);
//    }

 /*   public SGCommonProto.PrivacyMailResponse.Builder sendPrivacy(int sendType,Player sendPlayer,Player receiverPlayer, String content, long currentTime){

        SGCommonProto.PrivacyMailResponse.Builder r = SGCommonProto.PrivacyMailResponse.newBuilder();

        //获取对方的黑名单列表
        PrivacyDB privacyDB = new PrivacyDB();
        privacyDB.id =  ++mailNumber;
        privacyDB.content = content;

        privacyDB.sendTime = currentTime;
        privacyDB.isRead = false;
        List<DialogueData> list = new ArrayList<>();
        DialogueData dialogueData = new DialogueData();
        dialogueData.content = content;
        dialogueData.sendTime = currentTime;
        if(sendType == E_MailSenderType.MAIL_TYPE_SENDER.getCode()){
            dialogueData.senderType = E_MailSenderType.MAIL_TYPE_SENDER;
            privacyDB.targetIndex =  receiverPlayer.getObjectIndex();
            privacyDB.nickName =  receiverPlayer.getName();

        }else{
            dialogueData.senderType = E_MailSenderType.MAIL_TYPE_RECEIVER;
            privacyDB.targetIndex = sendPlayer.getObjectIndex();
            privacyDB.nickName = sendPlayer.getName();
        }
        list.add(dialogueData);
        privacyDB.replyList = list;


        if(sendType == E_MailSenderType.MAIL_TYPE_RECEIVER.getCode()){
//            MailInfo mailInfo = receiverPlayer.getExtInfo(MailInfo.class);
//            if(mailInfo.privacyList.size() > 20)
//                mailInfo.privacyList.remove(0);
//            mailInfo.privacyList.add(privacyDB);

            if(privacyList.size() > 20)
                privacyList.remove(0);
            privacyList.add(privacyDB);
            r.setTargetIndex(sendPlayer.getObjectIndex());
            r.setNickName(sendPlayer.getName());
            r.setTargetAvatar(sendPlayer.avatar);
        }else {
            if(privacyList.size() > 20)
                privacyList.remove(0);
            privacyList.add(privacyDB);
            r.setTargetAvatar(receiverPlayer.avatar);
            r.setNickName(receiverPlayer.getName());
            r.setTargetIndex(receiverPlayer.getObjectIndex());
        }
        System.out.println("privacyList:" + privacyList.toString());
        r.setPrivacyMailId(privacyDB.id);
        r.setSendTime(currentTime);
        r.setContent(content);
        r.setIsRead(false);
        return r;


    }*/



    public PrivacyDB sendPrivacy(int sendType,Player targetPlayer,Player player,String content, long currentTime,MailInfo mailInfo,int targetId){

        PrivacyDB privacyDB = new PrivacyDB();
        privacyDB.id =  ++ mailNumber;
        privacyDB.content = content;
        privacyDB.sendTime = currentTime;
        privacyDB.isRead = false;
        privacyDB.targetIndex =  targetPlayer.getObjectIndex();
        privacyDB.nickName =  targetPlayer.getName();
        privacyDB.targetId = targetId;

        List<DialogueData> list = new ArrayList<>();
        DialogueData dialogueData = new DialogueData();
        dialogueData.content = content;
        dialogueData.sendTime = currentTime;
        dialogueData.senderType = E_MailSenderType.forNumber(sendType);
        dialogueData.targetIndex = targetPlayer.getObjectIndex();
        list.add(dialogueData);


        privacyDB.replyList = list;

        if(mailInfo.privacyList.size() > 20)
            mailInfo.privacyList.remove(0);
        mailInfo.privacyList.add(privacyDB);

        checkRedPointRemindAllCondition(DataFactory.currentTime);

        return privacyDB;
//        System.out.println("接收放存储的数据privacyList:" + mailInfo.privacyList.toString());
    }


    public SGCommonProto.PrivacyMailResponse.Builder sendPrivacyResponse(int sendType,Player targetPlayer,Player player, String content, long currentTime,MailInfo mailInfo){
        SGCommonProto.PrivacyMailResponse.Builder r = SGCommonProto.PrivacyMailResponse.newBuilder();
        //获取对方的黑名单列表
        PrivacyDB privacyDB = new PrivacyDB();
        privacyDB.id =  ++mailNumber;
        privacyDB.content = content;
        privacyDB.sendTime = currentTime;
        privacyDB.isRead = false;
        privacyDB.targetIndex = targetPlayer.getObjectIndex();
        privacyDB.nickName = targetPlayer.getName();

        List<DialogueData> list = new ArrayList<>();
        DialogueData dialogueData = new DialogueData();
        dialogueData.content = content;
        dialogueData.sendTime = currentTime;
        dialogueData.senderType = E_MailSenderType.forNumber(sendType);
        dialogueData.targetIndex = player.getObjectIndex();
        list.add(dialogueData);


        privacyDB.replyList = list;

        if(mailInfo.privacyList.size() > 20)
            mailInfo.privacyList.remove(0);
        mailInfo.privacyList.add(privacyDB);
        r.setTargetIndex(targetPlayer.getObjectIndex());
        r.setNickName(targetPlayer.getName());
        r.setTargetAvatar(targetPlayer.avatar);

//        System.out.println("发送方存储的数据privacyList:" + mailInfo.privacyList.toString());
        r.setPrivacyMailId(privacyDB.id);
        r.setSendTime(currentTime);
        r.setContent(content);
        r.setIsRead(false);
        return r;
    }

    public TwoTuple<SGCommonProto.PrivacyMailResponse.Builder,PrivacyDB> sendPrivacyResponseTwo(int sendType,Player targetPlayer,Player player, String content, long currentTime,MailInfo mailInfo){
        SGCommonProto.PrivacyMailResponse.Builder r = SGCommonProto.PrivacyMailResponse.newBuilder();
        //获取对方的黑名单列表
        PrivacyDB privacyDB = new PrivacyDB();
        privacyDB.id =  ++mailNumber;
        privacyDB.content = content;
        privacyDB.sendTime = currentTime;
        privacyDB.isRead = false;
        privacyDB.targetIndex = targetPlayer.getObjectIndex();
        privacyDB.nickName = targetPlayer.getName();

        List<DialogueData> list = new ArrayList<>();
        DialogueData dialogueData = new DialogueData();
        dialogueData.content = content;
        dialogueData.sendTime = currentTime;
        dialogueData.senderType = E_MailSenderType.forNumber(sendType);
        dialogueData.targetIndex = player.getObjectIndex();
        list.add(dialogueData);


        privacyDB.replyList = list;

        if(mailInfo.privacyList.size() > 20)
            mailInfo.privacyList.remove(0);
        mailInfo.privacyList.add(privacyDB);
        r.setTargetIndex(targetPlayer.getObjectIndex());
        r.setNickName(targetPlayer.getName());
        r.setTargetAvatar(targetPlayer.avatar);

//        System.out.println("发送方存储的数据privacyList:" + mailInfo.privacyList.toString());
        r.setPrivacyMailId(privacyDB.id);
        r.setSendTime(currentTime);
        r.setContent(content);
        r.setIsRead(false);

        TwoTuple<SGCommonProto.PrivacyMailResponse.Builder,PrivacyDB> twoTuple = new TwoTuple<>();
        twoTuple.setFirst(r);
        twoTuple.setSecond(privacyDB);
        return twoTuple;
    }

    //检查私信列表是否存在此私信
    public boolean checkPrivacyIsExists( int privacyId){
        for(PrivacyDB privacyDB : privacyList){
            if(privacyDB.id == privacyId)
                return true;
        }
        return false;
    }
}
